<?php
namespace app\admin\controller;

use think\console\Command;
use think\console\Input;
use think\console\Output;

use think\Pgettext\Mo;
use think\Pgettext\Po;
use think\Pgettext\Stringset;

class mopoCommand extends Command
{
    protected function configure()
    {
        $this->addArgument('url');       //处理文件路径
        $this->addArgument('flag');       //是否设置标准翻译0或者1
        $this->setName('mopo:upload')
            ->setDescription('upload mo file to mysql');
    }

    //获取连接下的所有mo po文件
    public function folder_list($dir){
        $dir .= substr($dir, -1) == '/' ? '' : '/';
        $dirInfo = array();
        foreach (glob($dir.'*') as $v) {
            if(substr($v,strlen($v)-3,strlen($v))==".mo"){
                $list=explode('/',$v);
                $language="";
                foreach($list as $k => $i){
                    if($i=="LC_MESSAGES"){
                        $language=$list[$k-1];
                    }
                }
                if(!empty($language)){
                    $dirInfo['mo'][$language][] = $v;
                }
            }
            if(substr($v,strlen($v)-3,strlen($v))==".po"){
                $list=explode('/',$v);
                $language="";
                foreach($list as $k => $i){
                    if($i=="LC_MESSAGES"){
                        $language=$list[$k-1];
                    }
                }
                if(!empty($language)){
                    $dirInfo['po'][$language][] = $v;
                }
            }
            if(is_dir($v)){
                $dirInfo = array_merge($dirInfo, $this->folder_list($v));
            }
        }
        return $dirInfo;
    }

    public function addFile($stringset,$language,$filename,$flag=0){
        foreach($stringset->catalog() as $key => $val){
            if(!empty($val['id'])){
                $msgid=addslashes($val['id']);   //字符串转义     反转义函数stripslashes
                $where_msgid=addslashes($msgid);		//数据库查询时候要多加一次转义
                $isExists=db("msgid")->where("msgid='{$where_msgid}'")->find();
                //msgid表添加数据
                if(empty($isExists)){
                    $dataMsgid['msgid']=$msgid;
                    $msgid_id=db("msgid")->insertGetId($dataMsgid);
                }else
                    $msgid_id=$isExists['id'];
                if(!empty($val['strings'][0])){  //没翻译的不加入数据库
                    //msgstr表添加数据
                    $isExistsStr=db("msgstr")->where("msgid_id={$msgid_id} and language='{$language}'")->select();
                    if(empty($isExistsStr)){
                        $dateMsgstr['msgid_id']=$msgid_id;   			//msgid_id
                        $dateMsgstr['msgstr']=addslashes($val['strings'][0]);  //转义
                        $dateMsgstr['language']=$language;
                        $dateMsgstr['flag']=1;
                        $dateMsgstr['file_name']=substr($filename,0,strlen($filename)-3);		//文件名称
                        db("msgstr")->insert($dateMsgstr);
                    }else{
                        $is_same=0;	$is_same_id=0;	//判断是否为相同翻译
                        foreach($isExistsStr as $k => $v){
                            if($v['msgstr']==addslashes($val['strings'][0])){
                                $is_same=1;			//存在相同翻译
                                $is_same_id=$v['id'];
                            }
                        }
                        if($is_same==0){
                            if($flag){	//如果是标准翻译，把其他翻译设为非标准
                                db("msgstr")->where("msgid_id={$msgid_id} and language='{$language}'")->update(array("flag"=>0));
                            }
                            $dateMsgstr['msgid_id']=$msgid_id;   			//msgid_id
                            $dateMsgstr['msgstr']=addslashes($val['strings'][0]);  //转义
                            $dateMsgstr['language']=$language;
                            $dateMsgstr['flag']=$flag;
                            $dateMsgstr['file_name']=substr($filename,0,strlen($filename)-3);		//文件名称
                            db("msgstr")->insert($dateMsgstr);
                        }else{
                            if($flag){  //在设为标准翻译，且已存在相同的情况下，吧相同这个设为标准翻译，其他设置为0
                                db("msgstr")->where("msgid_id={$msgid_id} and language='{$language}'")->update(array("flag"=>0));
                                db("msgstr")->where("id={$is_same_id}")->update(array("flag"=>1));
                            }
                        }
                    }
                }
            }
        }
    }
    //运行函数
    protected function execute(Input $input, Output $output)
    {
        $args = $input->getArguments();
        $mo = new Mo();
        $po = new Po();
        if(empty($args['url'])) {
            $dir = ROOT_PATH . "uploads/mopo/";
        }else{
            $dir =$args['url'];
        }
        if(empty($args['flag'])) {
            $flag = 0;
        }else{
            $flag = 1;
        }
        $file=$this->folder_list($dir); //获取路径下的所有mopo文件
        if(empty($file)){
            print_r($dir." not find mopo file");exit;
        }
        //print_r($file);exit;
        $mosum=0;$posum=0;
        foreach($file['mo'] as $language => $fval) {
            foreach($fval as $k => $f){
                $mosum++;
                $fil_name=basename($f);
                $stringset=$mo->fromFile($f);
                print_r($f);print_r("\n");
                $this->addFile($stringset,$language,$fil_name,$flag);
            }

        }
        foreach($file['po'] as $language => $fval) {
            foreach($fval as $k => $f){
                $posum++;
                $fil_name=basename($f);
                $stringset=$po->fromFile($f);
                print_r($f);print_r("\n");
                $this->addFile($stringset,$language,$fil_name,$flag);
            }
        }
        if($mosum){
            print_r('Import '.$mosum.' mo files');print_r("\n");
        }
        if($posum){
            print_r('Import '.$posum.' mo files');print_r("\n");
        }
        print_r('ok');
    }
}